<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <%--<script type="text/javascript" src="js/jquery2.js"></script>--%>
    <%--<script type="text/javascript" src="js/jquery2.sorted.js"></script>--%>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script type="text/javascript" src="js/ckform.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <style type="text/css">
        body {
            font-size: 20px;
            padding-bottom: 40px;
            background-color: #e9e7ef;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media ( max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
    </style>
</head>
<body>
<form class="form-inline definewidth m20" action="#" method="get">
    <a href="documentAdd.jsp">发布博客</a>

</form>
<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr align="center">
        <th>博客标题</th>
        <th>博客类别</th>
        <th>作者</th>
        <th>发布日期</th>
        <th>管理菜单</th>
    </tr>
    </thead>
    <tbody id="tbody">
    <%--存放行数据--%>
    </tbody>
    <tfoot id="tfoot">
    <%--设置分页信息--%>
    </tfoot>
</table>

<script type="text/javascript">
    var pageSize = 3;

    //首页
    function firstPage() {
        loadBlogListWithPage(1, pageSize);
    }

    //上一页
    function prePage(pagenum) {
        if (pagenum == 0) {
            loadBlogListWithPage(1, pageSize);
        } else {
            loadBlogListWithPage(pagenum, pageSize);
        }
    }

    //下一页
    function nextPage(pagenum, maxnum) {
        console.info("next page...")
        if (pagenum == 0) {
            loadBlogListWithPage(maxnum, pageSize);
        } else {
            loadBlogListWithPage(pagenum, pageSize);
        }
    }

    //尾页
    function tailPage(pagenum) {
        loadBlogListWithPage(pagenum, pageSize);
    }

    //加载博客列表：
    function loadBlogListWithPage(currentPage, pageSize) {
        //tbody
        $("#tbody").empty();
        $("#tfoot").empty();

        //发送ajax请求：
        $.ajax({
            type: "POST",
            url: "/blog/findAll?currentPage=" + currentPage + "&pageSize=" + pageSize,
            dataType: "json",
            success: function (result) {
                $(result.list).each(function (index, item) {
                    //重置表单：
                    var $tr = `<tr align="center">
                            <td>` + item.btitle + `</td>
                            <td>` + item.typename + `</td>
                            <td>` + item.uname + `</td>
                            <td>` + item.date + `</td>
                            <td>
                            <a href="javascript:deleteInfo(` + item.bid + `)">删除 </a>
                            <a href="blog_detail.jsp?bid=` + item.bid + `">博客详情</a>
                            </td>
                           </tr>`;
                    $("#tbody").append($tr);
                });

                //设置分页信息：
                //分页
                var pageInfo = `<tr>
                                <td colspan="5">
                                <span>共有` + result.pages + `页,当前是第` + result.pageNum + `页</span> &nbsp;&nbsp;
                                <a href='javascript:void(0);' onclick='firstPage()'>首页</a>
                                <a href='javascript:void(0);' onclick='prePage(` + result.prePage + `)'>上一页</a>
                                <a href='javascript:void(0);' onclick='nextPage(` + result.nextPage + `,` + result.pages + `)'>下一页</a>
                                <a href='javascript:void(0);' onclick='tailPage(` + result.pages + `)'>尾页</a>
                                </td>
								</tr>`;
                $("#tfoot").append(pageInfo);
            }
        });
    }

    //删除
    function deleteInfo(bid) {
        $.get("/blog/deleteInfo?bid=" + bid, function (result) {
            var flag = window.confirm("您确定要删除该条数据吗？");
            if (flag) {
                console.info(result);
                if (result.msg) {
                    alert(result.msg);
                    window.location.reload();
                } else {
                    alert(result.error);
                }
            }
        });
    }

    $(function () {
        //首页
        firstPage();
        //上一页
        prePage(pagenum);
        //下一页
        nextPage(pagenum, maxnum);
        //尾页
        tailPage(pagenum);
        //加载博客列表
        loadBlogListWithPage(currentPage, pageSize);
        //删除
        deleteInfo(bid);

    });
</script>
</body>
</html>
